
package com.seekting.study;

public class ThreadLocalDemo implements Runnable {

    private static final ThreadLocal<Student> THREAD_LOCAL = new ThreadLocal<ThreadLocalDemo.Student>();

    public static void main(String[] args) {
        ThreadLocalDemo demo = new ThreadLocalDemo();
        new Thread(demo).start();
        new Thread(demo).start();

    }

    private static class Student {
        int age;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    @Override
    public void run() {

        Student s = THREAD_LOCAL.get();
        if (s == null) {
            s = new Student();
            s.setAge(20);
            THREAD_LOCAL.set(s);
        }
        System.out.println(s.hashCode());
    }
}
